package lib

import (
	"GameServer/Common/data"
	"GameServer/Game/errors"
	"GameServer/Game/m"
	"fmt"
)

// 获取物品ID列表
func GetUserSpecialBoxGoodsIds(u *m.User) (GoodsLst []m.SpecailBoxGood, err error) {
	GoodsLst = make([]m.SpecailBoxGood, 0)
	layerT, ok := getLayerT(uint64(u.MaxPos.LockBlockTid), uint64(u.MaxPos.LayerId))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, fmt.Sprintf("没有找到层配置信息:Pos%v", u.MaxPos))
		// 没有找到层配置信息，给玩家第一关的道具算了
		GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 0, Tid: 18010101, IsNew: 0}) // 0-新 1-旧
		GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 1, Tid: 18010102, IsNew: 1}) // 0-新 1-旧
		GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 2, Tid: 18010103, IsNew: 0}) // 0-新 1-旧
		GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 3, Tid: 18010104, IsNew: 1}) // 0-新 1-旧
		GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 4, Tid: 18010105, IsNew: 0}) // 0-新 1-旧
		return
	}
	// 玩家最大章节
	userPage := layerT.Page
	fmt.Println(userPage)
	lgoodtid, _ := RandHeroGoodNum(u, 5, int64(userPage))
	for idx, item := range lgoodtid {
		GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: int64(idx), Tid: item, IsNew: 0})
	}
	// GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 0, Tid: 18010101, IsNew: 0}) // 0-新 1-旧
	// GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 1, Tid: 18010102, IsNew: 1}) // 0-新 1-旧
	// GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 2, Tid: 18010103, IsNew: 0}) // 0-新 1-旧
	// GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 3, Tid: 18010104, IsNew: 1}) // 0-新 1-旧
	// GoodsLst = append(GoodsLst, m.SpecailBoxGood{Id: 4, Tid: 18010105, IsNew: 0}) // 0-新 1-旧
	// kvs, res1, err := getBoxRewards(utils.Uint64(goodsT.Effect), layerT.Page)
	// TODO ---- 根据玩家章节限制，和其他需求（章节权重，英雄权重，道具权重，优先获得没有的道具）生成道具列表

	// 这里暂时只是随机生成章节以下的道具
	// -- start
	// 18010101
	// tplHeros, ok := data.GetAllDatas("Heros")
	// if !ok {
	// 	err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到角色表,Heros")
	// 	// 没有找到层配置信息，给玩家第一关的道具算了
	// 	GoodsLst = append(GoodsLst, 18010101)
	// 	GoodsLst = append(GoodsLst, 18010102)
	// 	GoodsLst = append(GoodsLst, 18010103)
	// 	GoodsLst = append(GoodsLst, 18010104)
	// 	GoodsLst = append(GoodsLst, 18010105)
	// 	return
	// }
	// herosIds := make([]int64, 0)
	// for _, value := range tplHeros {
	// 	theHero := value.(data.Hero)
	// 	if theHero.Page <= userPage {
	// 		herosIds = append(herosIds, int64(theHero.Id))
	// 	}
	// }
	// baseId := int64(18000000)
	// randomPageId := utils.RandomInt(1, int(userPage))
	// if randomPageId == 0 {
	// 	randomPageId = 1
	// }
	// baseId = baseId + int64(randomPageId)
	// -- end
	return
}

// 修改玩家5选1宝箱数据，由GiveGoodsByKv调用，当箱子大类为3箱子，小类为5选1箱子
func GiveSpBoxToUser(u *m.User) (err error) {
	userSPcontent, theTid := u.AddOrCreateSpBoxContent()
	goods, err := GetUserSpecialBoxGoodsIds(u)
	if err != nil {

	}
	userSPcontent.SpBoxContent[theTid].Tid = theTid
	userSPcontent.SpBoxContent[theTid].Goods = make([]m.SpecailBoxGood, 0)
	for _, value := range goods {
		userSPcontent.SpBoxContent[theTid].Goods = append(userSPcontent.SpBoxContent[theTid].Goods, value)
	}
	defer userSPcontent.Update(userSPcontent)
	return
}

// 玩家选择道具
func GiveSpBoxItemByUserChoice(u *m.User, contentTid int64, choiceId int64) (goodTid int64, err error) {
	userSpContent := u.GetUserSpBoxContent()
	if userSpContent == nil {
		err = errors.NewErrcode(data.Errcode.NotFound, "找不到玩家SpBox数据")
		return
	}
	theContent, find := userSpContent.SpBoxContent[contentTid]
	if find == false {
		err = errors.NewErrcode(data.Errcode.NotFound, fmt.Sprintf("找不到玩家SpBox数据,Tid为 %d", contentTid))
		return
	}
	goodTid = 0
	for _, value := range theContent.Goods {
		if value.Id == choiceId {
			goodTid = value.Tid
		}
	}
	if goodTid == 0 {
		err = errors.NewErrcode(data.Errcode.NotFound, fmt.Sprintf("找不到玩家SpBox数据,Tid为 %d,choiceId %d", contentTid, choiceId))
		return
	}
	// 给道具 交给playerModule的specialbox去做
	// 修改数据
	delete(userSpContent.SpBoxContent, contentTid)
	defer userSpContent.Update(userSpContent)
	return goodTid, nil
}
